package actions;

import java.sql.SQLException;

import java.util.*;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
//import forms.StudentForm;
import model.ReviewerModel;

public class ReviewerAction extends Action {

	private static final boolean DEBUG = true;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
		
		String path = mapping.getPath();
		
		String user = request.getUserPrincipal().getName();
		
		if (DEBUG) {
			System.err.println("ReviewerAction: " + path);
			System.err.println("Reviewer Logged in: " + user);
		}

		// Retrieve all reviewers
		//TODO: NEED TO CHANGE THIS CONSTANT VALUE TO WHOEVER IS LOGGING IN
		RowSet appReviews = ReviewerModel.getAllAppReviews(user);
		
		
		Vector<String> applicationReviews = new Vector<String>();
		Vector<String> applicantFirstName = new Vector<String>();
		Vector<String> applicantMiddleName = new Vector<String>();
		Vector<String> applicantLastName = new Vector<String>();
		
		try {
			while(appReviews.next())
			{
				String full_name = appReviews.getString("first_name") + " " 
									+ appReviews.getString("middle_name") + " "
									+ appReviews.getString("last_name");

				applicationReviews.add(full_name);				
				
				applicantFirstName.add(appReviews.getString("first_name"));
				applicantMiddleName.add(appReviews.getString("middle_name"));
				applicantLastName.add(appReviews.getString("last_name"));
				
				System.err.println("Applicant found for reviewer: " + full_name);
			}
			
		} catch (SQLException ex) {
			throw new DbException(ex);
		}

		
		// Store the Vectors in the request scope
		request.setAttribute("applicationReviews", applicationReviews);
		request.setAttribute("applicantFirstName", applicantFirstName);
		request.setAttribute("applicantMiddleName", applicantMiddleName);
		request.setAttribute("applicantLastName", applicantLastName);

		return mapping.findForward("success");
	}
}